package com.alks.function.service.impl.biocontrol.deptwork;


import com.alks.common.data.thread.UserIdThread;
import com.alks.entity.data.entity.PsLineCut;
import com.alks.entity.data.response.ResponseInfo;
import com.alks.function.data.dto.biocontrol.webwork.CutLineDto;
import com.alks.function.mapper.biocontrol.deptwork.CutLineMapper;
import com.alks.function.service.biocontrol.deptwork.ICutLineService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

@Service
@Slf4j
@RequiredArgsConstructor
public class CutLineServiceImpl implements ICutLineService {

    private final CutLineMapper cutLineMapper;

    /**
     * 基础资料设定：冲裁部门资料定义查询
     * @return
     */
    @Override
    public ResponseInfo list() {
        String companyId = UserIdThread.get().getCompanyId();
        List<CutLineDto> list = cutLineMapper.list(companyId);
        return ResponseInfo.ok(list);
    }

    /**
     * 冲裁部门资料定义创建
     * @param cutLine
     * @return
     */
    @Override
    public ResponseInfo addStitchLine(CutLineDto cutLine) {
        // 验证
        ResponseInfo validationResult = validate(cutLine);
        if (validationResult != null) {
            return validationResult;
        }

        //根据iD查询该数据
        String companyId = UserIdThread.get().getCompanyId();
        List<CutLineDto> list = cutLineMapper.list(companyId);
        if(ObjectUtils.isNotEmpty(list)){
            for (CutLineDto dto : list) {
                //不能存在相同的序号
                if (dto.getSeqNo().equals(cutLine.getSeqNo())) {
                    return ResponseInfo.error("序号不能重复");
                }
                //不能存在相同的生产线名称
                if (dto.getLine().equals(cutLine.getLine())) {
                    return ResponseInfo.error("生产线名称不能重复");
                }
            }
        }

        //赋值
        String userId = UserIdThread.get().getUserId();
        cutLine.setCompanyId(companyId);
        cutLine.setUserId(userId);
        cutLine.setSysDate(LocalDate.now());

        int count = cutLineMapper.insert(cutLine);

        if(count > 0){
            return ResponseInfo.ok("添加成功");
        }else{
            return ResponseInfo.error("添加失败");
        }
    }

    /*冲裁部门资料定义更新*/
    @Override
    public ResponseInfo updateByRowId(CutLineDto cutLine) {
        //验证
        ResponseInfo validationResult = validate(cutLine);
        if (validationResult != null) {
            return validationResult;
        }

        //根据iD查询该数据
        String companyId = UserIdThread.get().getCompanyId();
        List<CutLineDto> list = cutLineMapper.list(companyId);
        if(ObjectUtils.isNotEmpty(list)){
            for (CutLineDto dto : list) {
                //不能存在相同的序号
                if (dto.getSeqNo() == cutLine.getSeqNo()) {
                    return ResponseInfo.error("序号不能重复");
                }
                //不能存在相同的生产线名称
                if (dto.getLine().equals(cutLine.getLine())) {
                    return ResponseInfo.error("生产线名称不能重复");
                }
            }
        }

        String userId = UserIdThread.get().getUserId();
        cutLine.setCompanyId(companyId);
        cutLine.setUserId(userId);
        cutLine.setSysDate(LocalDate.now());
        int count = cutLineMapper.updateByRowId(cutLine);
        if(count > 0){
            return ResponseInfo.ok("更新成功");
        }else{
            return ResponseInfo.error("更新失败");
        }
    }

    /**
     * 冲裁部门资料定义删除
     * @param cutLine
     * @return
     */
    @Override
    public ResponseInfo deleteCutLineByRowId(int id) {
        int count = cutLineMapper.deleteByRowId(id);

        if(count > 0){
            return ResponseInfo.ok("删除成功");
        }else{
            return ResponseInfo.error("删除失败");
        }
    }


    /*判断*/
    public ResponseInfo validate(CutLineDto cutLine) {
        // 冲裁名称不能为空
        if (StringUtils.isEmpty(cutLine.getLine())) {
            return ResponseInfo.error("冲裁名称不能为空");
        }

        // 序号不能小于0
        if (cutLine.getSeqNo().signum() < 0) {
            return ResponseInfo.error("序号不能小于0");
        }
        if (cutLine.getSeqNo() == null) {
            return ResponseInfo.error("序号不能为空");
        }

        // 裁断机台数量不能为空
        if (cutLine.getMachineNum() == null) {
            return ResponseInfo.error("裁断机台数量不能为空");
        }
        // 裁断机台数量不能小于0
        if (cutLine.getMachineNum().signum() < 0) {
            return ResponseInfo.error("裁断机台数量不能小于0");
        }

        // 机台日刀数不能为空
        if (cutLine.getStdQty() == null) {
            return ResponseInfo.error("机台日刀数不能为空");
        }
        // 机台日刀数不能小于0
        if (cutLine.getStdQty().signum() < 0) {
            return ResponseInfo.error("机台日刀数不能小于0");
        }

        // 所有验证都通过
        return null;
    }


}
